package com.kuzan.autocloud.client.sys.config.datainit;

import com.kuzan.autocloud.client.sys.config.Initialize;
import com.kuzan.autocloud.common.model.menu.MenuGroup;
import com.kuzan.autocloud.common.model.menu.MenuItem;
import com.kuzan.autocloud.common.model.menu.MenuRoot;
import com.kuzan.autocloud.common.model.system.ACPermission;
import com.kuzan.autocloud.common.repository.menu.MenuGroupRepository;
import com.kuzan.autocloud.common.repository.menu.MenuItemRepository;
import com.kuzan.autocloud.common.repository.menu.MenuRootRepository;
import com.kuzan.autocloud.common.repository.system.ACPermissionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.util.Arrays;

import static com.kuzan.autocloud.client.sys.config.datainit.InitOrganizationService.Id_Person_Fhs;

/**
 * <p>Description: </p>
 *
 * @author kuzank 2019/12/2
 */
@Service
@Order(1)
public class InitMenuService implements Initialize {

    @Autowired
    MenuRootRepository rootRepository;
    @Autowired
    MenuGroupRepository groupRepository;
    @Autowired
    MenuItemRepository itemRepository;
    @Autowired
    ACPermissionRepository ACPermissionRepository;


    @Override
    public void initializedb() throws Exception {

        MenuRoot rootDashboard = MenuRoot.of("主导航", "10001");
        MenuRoot rootAutoCloud = MenuRoot.of("Auto-Cloud", "10002");


        MenuGroup groupDeveloper = MenuGroup.of("开发者平台", "11001", rootAutoCloud.getId(), "", MenuRoot.MenuPermissionScope.specify);
        MenuGroup groupDBManager = MenuGroup.of("数据库管理", "11002", rootAutoCloud.getId(), "", MenuRoot.MenuPermissionScope.specify);


        MenuItem itemMenuManage = MenuItem.of("菜单配置", "12003", groupDeveloper.getId(), MenuRoot.MenuPermissionScope.specify);
        itemMenuManage.setType(MenuRoot.MenuType.Url.getName());
        itemMenuManage.setUrl("/");

        /**
         * Id_Person_Fhs 的菜单权限
         */
        ACPermission ACPermission1 = ACPermission.of("13001", groupDeveloper.getId(), Id_Person_Fhs, ACPermission.ACPermissionType.Person);
        ACPermission ACPermission2 = ACPermission.of("13002", groupDBManager.getId(), Id_Person_Fhs, ACPermission.ACPermissionType.Person);
        ACPermission ACPermission3 = ACPermission.of("13003", itemMenuManage.getId(), Id_Person_Fhs, ACPermission.ACPermissionType.Person);


        rootRepository.saveAll(
                Arrays.asList(rootDashboard, rootAutoCloud)
        );

        groupRepository.saveAll(
                Arrays.asList(groupDeveloper, groupDBManager)
        );

        itemRepository.saveAll(
                Arrays.asList(itemMenuManage)
        );

        ACPermissionRepository.saveAll(
                Arrays.asList(ACPermission1, ACPermission2, ACPermission3)
        );
    }
}